<?php
	require_once('./config.php');
	require_once('./network.php');
	require_once('./security.php');

	function OAuth_1()//request_token, redirect to OAuth Server
	{
		global $oauth_url;

		$request_token=RequestToken();

		if($request_token['content']==FALSE)//cURL失败
		{
			$error='Network Failure';
			FailPage($error);
		}
		else if($request_token['http_code'] == 200)//Got request_token
		{
			session_start();
			$_SESSION['oauth_token_secret']=$request_token['oauth_token_secret'];
			session_write_close();

			header('Location: ' . $oauth_url . 'authorize?oauth_token=' . $request_token['oauth_token']);
		}
		else//request_token error
		{
			$error='Fail when getting request_token<br />';
			$error .= $request_token['http_code'] . '<br />';
			$error .= $request_token['content'];
			FailPage($error);
		}

		return 0;
	}

	function OAuth_2($callback)//access_token
	{
		$access_token=AccessToken($callback['oauth_token'], $callback['oauth_token_secret'], $callback['oauth_verifier']);

		if($access_token['content']==FALSE)//cURL失败
		{
			$error='Network Failure';
			FailPage($error);
		}
		else if($access_token['http_code'] == 200)//Got access_token, OAuth成功
			SuccessPage($access_token['oauth_token'], $access_token['oauth_token_secret']);
		else//access_token error
		{
			$error='Fail when getting access_token<br />';
			$error .= $access_token['http_code'] . '<br />';
			$error .= $access_token['content'];
			FailPage($error);
		}

		return 0;
	}

	function RequestToken()
	{
		global $oauth_signature_method, $oauth_version, $oauth_url, $consumer_key, $consumer_secret, $oauth_callback;

		$oauth_timestamp=time();
		$oauth_nonce=$oauth_timestamp;

		$oauth_param=array('oauth_consumer_key'=>$consumer_key, 'oauth_signature_method'=>$oauth_signature_method, 'oauth_timestamp'=>$oauth_timestamp, 'oauth_nonce'=>$oauth_nonce, 'oauth_version'=>$oauth_version, 'oauth_callback'=>$oauth_callback);
		foreach($oauth_param as $key => $val)
			$oauth_param[$key]=urlencode($val);
		
		$method='POST';
		$url=$oauth_url . 'request_token';
		$secret=$consumer_secret . '&';

		$response=TwitterHttp($method, $url, $oauth_param, $secret);

		if($response['content']!=FALSE && $response['http_code']==200)
		{
			$content=explode('&', $response['content']);
			foreach ($content as $val)
			{
				$param=explode('=', $val);
				if(array_key_exists(1, $param))
					$response[$param[0]]=$param[1];
			}
		}

		return $response;
	}

	function AccessToken($oauth_token, $oauth_token_secret, $oauth_verifier)
	{
		global $oauth_signature_method, $oauth_version, $oauth_url, $consumer_key, $consumer_secret;

		$oauth_timestamp=time();
		$oauth_nonce=$oauth_timestamp;

		$oauth_param=array('oauth_consumer_key'=>$consumer_key, 'oauth_token'=>$oauth_token, 'oauth_signature_method'=>$oauth_signature_method, 'oauth_timestamp'=>$oauth_timestamp, 'oauth_nonce'=>$oauth_nonce, 'oauth_version'=>$oauth_version, 'oauth_verifier'=>$oauth_verifier);
		foreach($oauth_param as $key => $val)
			$oauth_param[$key]=urlencode($val);

		$method='POST';
		$url=$oauth_url . 'access_token';
		$secret=$consumer_secret . '&' . $oauth_token_secret;

		$response=TwitterHttp($method, $url, $oauth_param, $secret);

		if($response['content']!=FALSE && $response['http_code']==200)
		{
			$content=explode('&', $response['content']);
			foreach ($content as $val)
			{
				$param=explode('=', $val);
				if(array_key_exists(1, $param))
					$response[$param[0]]=$param[1];
			}
		}

		return $response;
	}

	function FailPage($error)
	{
		echo '<html>' . "\r\n";
		echo '	<head>' . "\r\n";
		echo '		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . "\r\n";
		echo '		<meta http-equiv="Refresh" content="5; url=./" />' . "\r\n";
		echo '		<title>Twitter Feed</title>' . "\r\n";
		echo '	</head>' . "\r\n";
		echo '	<body>' . "\r\n";
		echo '		' . $error . '<br /><br />' . "\r\n";
		echo '		Redirect to the home page after 5 seconds.<br />' .  "\r\n";
		echo '		Click <a href="./">here</a> if your browser does not automatically redirect.<br />' .  "\r\n";
//		echo '		Click <a href="./">here</a> back to the home page<br />' .  "\r\n";
		echo '	</body>' . "\r\n";
		echo '</html>' . "\r\n";

		return 0;
	}

	function SuccessPage($oauth_token, $oauth_token_secret)
	{
		global $base_url;

		$oauth_token=TokenEncrypt($oauth_token);
		$oauth_token_secret=TokenEncrypt($oauth_token_secret);

		session_start();
		$_SESSION['oauth_token']=$oauth_token;
		$_SESSION['oauth_token_secret']=$oauth_token_secret;
		session_write_close();

		$oauth_token=urlencode($oauth_token);
		$oauth_token_secret=urlencode($oauth_token_secret);

		$feed_addr=$base_url . 'feed.php';
		$feed_addr_full=$feed_addr . '?oauth_token=' . $oauth_token . '&oauth_token_secret=' . $oauth_token_secret;

		echo '<html>' . "\r\n";
		echo '	<head>' . "\r\n";
		echo '		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . "\r\n";
		echo '		<title>Twitter Feed - OAuth Success</title>' . "\r\n";
		echo '	</head>' . "\r\n";
		echo '	<body>' . "\r\n";
		echo '	 Your feed address is <br />' . "\r\n";
		echo '	 <a href="' . $feed_addr_full . '">' . $feed_addr_full . '</a><br /><br />' . "\r\n";
		echo '	 For security reason, you are recommended to <a href="./do.php?action=savecookie">SAVE COOKIE</a> and visit your feed using the address <br />' . "\r\n";
		echo '	 <a href="' . $feed_addr . '">' . $feed_addr . '</a><br /><br />' . "\r\n";
		echo '	</body>' . "\r\n";
		echo '</html>' . "\r\n";

		return 0;
	}
?>